import Vue from 'vue'
import App from './app.vue'
import Commons from './commons'
import Element from 'element-ui'
import moment from 'moment'
import 'element-ui/lib/theme-default/index.css'
import 'font-awesome/css/font-awesome.css'
import '../lib/lodash/lodash.min'
import '../lib/iconfont/aui-iconfont.css'
import './assets/css/common.css'

import router from './router'
import {save, read, clear} from './storage/session'
import store from './store'
Vue.use(Element)
Vue.use(Commons)

// /*引用canvas图标插件*/
// import jquery from '../static/jQuery.js'
// import jqplot from  '../static/jqplot.js'


import echarts from 'echarts'

Vue.prototype.$echarts = echarts

/*开放url*/
var open_menu_list = ['home','Profile', 'login','DeskTop', 'pwd','/NoPermission', '/Notfind'];
var menu_list = Object.assign([],open_menu_list);
var l = menu_list.length;
const MENU_LIST = 'MENU_LIST';
function ckMenu(menuList) {
  if (menuList instanceof Array) {
    if (menuList.length > 0)
      for (var i = 0; i < menuList.length; i++) {
        var menu = menuList[i];
        if(menu==undefined)
          continue;
        if (menu.child && menu.child.length > 0) {
          for (var c = 0; c < menu.child.length; c++) {
            var menu_ = menu.child[c];
            if (menu_ != undefined)
              ckMenu(menu_);
          }
        }
        if (menu.url)
          menu_list.push(menu.url);
        if (menu.pathList&&menu.pathList!=''){
          var pathList=menu.pathList.split(",");
          if(pathList!=null&&pathList.length>0){
            for(var i_=0;i_<pathList.length;i_++){
              menu_list.push(pathList[i_]);
            }
          }
        }
      }
  } else {
    if (menuList.url)
      menu_list.push(menuList.url);
      if (menuList.pathList&&menuList.pathList!=''){
        var pathList=menuList.pathList.split(",");
        if(pathList!=null&&pathList.length>0){
          for(var i_=0;i_<pathList.length;i_++){
            menu_list.push(pathList[i_]);
          }
        }
      }
    if (menuList.child) {
      ckMenu(menuList.child);
    }
  }
}

function ckAuth(pathName, path) {
  // if (menu_list.length == l) {
    var json = read(MENU_LIST);
    if (json == undefined || json == '') {
      return false;
    }
    menu_list = Object.assign([],open_menu_list);
    var menuList = JSON.parse(json);
    if (menuList && menuList.length > 0) {
      ckMenu(menuList);
    }
  // }
  // return menu_list.indexOf(pathName) >= 0 || (path != '' && menu_list.indexOf(path) >= 0);
  return true;
}
router.beforeEach((to, from, next) => {
  /**
   * 1.如果已经登陆，访问的是登陆页面，直接重定向到主页，如果不是则放开
   * 2.如果没有登陆，所有访问重定向到登录页
   */
  if (store.getters.getIsLogin) {
    if (to.path === '/login') {
      next('/');
    } else {
      if (to == undefined || to.name == undefined || to.name == null) {
        next('/Notfind');
        return;
      }
      //免权限访问
      if ((open_menu_list.indexOf(to.name)>=0||open_menu_list.indexOf(to.path)>=0)){
        next();
        return;
      }
      if(ckAuth(to.name,to.path)){//需要权限访问
        next();
      }else {
        next('/NoPermission');
        // router.push({path:'/noPermission'});
        // next({path:'/noPermission'});
      }
    }
  } else {
    if (to.path === '/login') {
      next();
    } else {
      next('/login')
    }
  }
})
Vue.filter('datetime', function (value, formatString) {
  formatString = formatString || 'YYYY-MM-DD HH:mm:ss';
  return moment(value).format(formatString);
});
Vue.filter('time', function (value, formatString) {
  formatString = formatString || 'HH:mm:ss';
  return moment(value).format(formatString);
});
Vue.filter('date', function (value, formatString) {
  formatString = formatString || 'YYYY-MM-DD';
  return moment(value).format(formatString);
});
Vue.filter('emos', function (emoMsg) {
  return Commons.$utils.emos(emoMsg);
});

Vue.http.options.emulateJSON = true;
Vue.http.options.headers = {
  'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
};

/* eslint-disable no-new
 * router 和 store 使用$router $store
 * */
new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
